package com.banshou.installs.ui.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.banshou.installs.R;
import com.banshou.installs.constant.Config;
import com.banshou.installs.databinding.FloorSetBinding;
import com.banshou.installs.model.dao.FloorSet;
import com.banshou.installs.model.dao.RequestSendLoucenInfo;
import com.banshou.installs.model.dao.ResponseLogin;
import com.banshou.installs.model.net.Observer;
import com.banshou.installs.model.net.ResponseInfo;
import com.banshou.installs.model.net.RetrofitManager2;
import com.banshou.installs.model.net.RxUtil;
import com.banshou.installs.ui.adapter.MMFloorSetAdapter;
import com.banshou.installs.utils.DialogUtils;
import com.banshou.installs.utils.ToastUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by cjq on 2018/4/18.
 * Email: stoic_yb@139.com
 * features:
 */
public class FloorSetActivity extends BaseActivity implements View.OnClickListener {
    private FloorSetBinding mBinding;
    private String startLc;
    private String endLc;
    private List<FloorSet> floorSetList;
    private Map<Integer, FloorSet> floorSetMap;
    private String jsonStr;
    private String eleId;
    private Dialog dialog;
    private DialogUtils dialogUtils;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.floor_set);
        initView();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initView() {
        mBinding.title.pagerTitle.setText("屏幕信息设置");
        dialogUtils = new DialogUtils();
        startLc = getIntent().getStringExtra(Config.STARTLC);
        endLc = getIntent().getStringExtra(Config.ENDLC);
        eleId = getIntent().getStringExtra(Config.ELEID);
        floorSetList = new ArrayList<>();
        floorSetMap = new ArrayMap<>();
        if (startLc != null && endLc != null) {
            for (int i = Integer.valueOf(startLc); i <= Integer.valueOf(endLc); i++) {
                if (i == 0) {
                    continue;
                }
                FloorSet floorSet = new FloorSet(i + "", i + "", false);
                floorSetList.add(floorSet);
                floorSetMap.put(i,floorSet);
            }
            jsonStr = new Gson().toJson(floorSetMap);
        }
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        MMFloorSetAdapter adapter = new MMFloorSetAdapter(this, R.layout.myfloorsetitem, floorSetList);
        mBinding.recyclerView.setAdapter(adapter);
        mBinding.buNext.setOnClickListener(this);
        mBinding.title.back.setOnClickListener(this);
        adapter.setMapListener(floorSetMap -> {
            jsonStr = null;
            jsonStr = new Gson().toJson(floorSetMap);
        });
    }

    private void setResult() {
        if (eleId != null && jsonStr != null) {
            dialogUtils.showAni(this);
            RequestSendLoucenInfo loucenInfo = new RequestSendLoucenInfo(eleId,jsonStr);
            RetrofitManager2.getInstance()
                    .create(ResponseInfo.class)
                    .setEleInfo(loucenInfo)
                    .compose(RxUtil.lifecycle(this))
                    .subscribe(new Observer<ResponseLogin>() {
                        @Override
                        protected void onNextStep(ResponseLogin responseLogin) {
                            dialogUtils.hideAni();
                            Intent intent = new Intent(FloorSetActivity.this, InstallHisActivity.class);
                            intent.putExtra(Config.FLOORSET_HISTORY, Config.FLOORSET_HISTORY);
                            startActivity(intent);
                            finish();
                        }

                        @Override
                        protected void onErrorResult() {
                            dialogUtils.hideAni();
                        }
                    });
//            Call<ResponseLogin> responseLoginCall = RetrofitManager.getInstance().create(ResponseInfo.class).setEleInfo(eleId, jsonStr);
//            responseLoginCall.enqueue(new Callback<ResponseLogin>() {
//                @Override
//                public void onResponse(Call<ResponseLogin> call, Response<ResponseLogin> response) {
//                    Logger.d(response.toString());
//                    ResponseLogin body = response.body();
//                    if (body != null && body.getResultCode() != null && body.getResultCode().equals("200")) {
//                        Intent intent = new Intent(FloorSetActivity.this, InstallHisActivity.class);
//                        intent.putExtra(Config.FLOORSET_HISTORY, Config.FLOORSET_HISTORY);
//                        startActivity(intent);
//                        finish();
//                    }else{
//                        ToastUtil.toast(Config.NONET);
//                    }
//                }
//
//                @Override
//                public void onFailure(Call<ResponseLogin> call, Throwable t) {
//                    ToastUtil.toast(Config.NONET);
//                }
//            });
        }else{
            ToastUtil.toast(Config.NONET);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.cancel:
                dialog.cancel();
                break;
            case R.id.confirm:
                dialog.cancel();
                setResult();
                break;
            case R.id.bu_next:
                dialog = new Dialog(FloorSetActivity.this, R.style.edit_AlertDialog_style);
                View view1 = LayoutInflater.from(FloorSetActivity.this).inflate(R.layout.del_dialog, null);
                TextView textView = view1.findViewById(R.id.tv_title);
                TextView textView2 = view1.findViewById(R.id.tv_mess);
                textView.setText("确认");
                textView2.setText("提交后不能修改，是否确认提交");
                dialog.setContentView(view1);
                dialog.show();
                Button confirm = view1.findViewById(R.id.confirm);
                Button cancel = view1.findViewById(R.id.cancel);
                confirm.setOnClickListener(FloorSetActivity.this);
                cancel.setOnClickListener(FloorSetActivity.this);
                break;
            case R.id.rl_cancel:
                finish();
                break;
        }
    }
}
